var express = require('express');
var router = express.Router();
// lowdb 内容知识 看test文件实例
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync(__dirname + '/../data/db.json')
const db = low(adapter)

const shortId = require('shortid')

/* GET home page. */
router.get('/', function(req, res, next) {
  const result = db.get('account').value()
  res.render('index', { title: '记账本' , data:result});
});
router.get('/create', function(req, res, next) {
  res.render('create', { title: '添加记账本' });
});

router.post('/add',function(req,res,next){
  let id = shortId.generate()
  db.get('account').push({id,...req.body}).write()
  res.render('success',{title:'添加成功',url:'/'})
})

router.get('/delete/:id',function(req,res,next){
  db.get('account').remove({id:req.params.id}).write()
  res.render('success',{title:'删除成功',url:'/'})
})

module.exports = router;
